package com.sny.service.impl;

import com.sny.concurrent.FileCallable;
import com.sny.constants.FileConstants;
import com.sny.dao.IFileStorageDao;
import com.sny.dto.FileStorageDto;
import com.sny.dto.FileUploadDTO;
import com.sny.emun.ResponseMsg;
import com.sny.exception.BizException;
import com.sny.model.FileStorageModel;
import com.sny.service.IFileStorageService;
import com.sny.strategy.enu.UploadModeEnum;
import com.sny.util.DateUtil;
import com.sny.util.FilePathUtil;
import com.sny.util.FileUtil;
import com.sny.util.RedisUtil;
import com.sny.vo.param.FileUploadRequestParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:yangxupeng
 * @Description:
 * @Date: 2020/7/31 9:57 上午
 * @Modified:
 */
@Slf4j
@Service
public class FileStorageServiceImpl implements IFileStorageService {

    @Autowired
    private IFileStorageDao fileStorageDao;

    @Override
    public FileStorageDto save(FileStorageDto fileStorage) {

        FileStorageModel fileStorageModel = new FileStorageModel();
        BeanUtils.copyProperties(fileStorage,fileStorageModel);
        fileStorageModel = fileStorageDao.save(fileStorageModel);
        BeanUtils.copyProperties(fileStorageModel,fileStorage);
        return fileStorage;
    }


    @Override
    public FileStorageDto findByName(String fileName) {
        FileStorageDto fileStorageDto = new FileStorageDto();
        BeanUtils.copyProperties(fileStorageDao.findByName(fileName),fileStorageDto);
        return fileStorageDto;
    }

    @Override
    public Resource renderBinary(String fileName) {
        try {
            Resource resource = new UrlResource("file:"+fileName);

            if(resource.exists()) {
                return resource;
            } else {
                throw new RuntimeException("File not found " + fileName);
            }
        } catch (MalformedURLException ex) {
            throw new RuntimeException("File not found " + fileName, ex);
        }
    }

    @Override
    public FileStorageDto findByMd5(String md5) {
        FileStorageModel fileStorageModel = fileStorageDao.findByMD5(md5);
        if( fileStorageModel == null ){
            return null;
        }
        FileStorageDto fileStorageDto = new FileStorageDto();
        BeanUtils.copyProperties(fileStorageModel,fileStorageDto);
        return fileStorageDto;
    }

    @Override
    public void delete(String fileName) {
        fileStorageDao.delete(fileStorageDao.findByName(fileName));
    }

    @Override
    public List<FileStorageDto> findFileByNames(String fileIds) {
        List<FileStorageDto> fileStorageDtos = new ArrayList<FileStorageDto>();
        if(StringUtils.isEmpty(fileIds)){
            return fileStorageDtos;
        }
        List<FileStorageModel> fileStorageModelList = fileStorageDao.findByNameIn(Arrays.asList(fileIds.split(",")));
        for (FileStorageModel fileStorage:fileStorageModelList) {
            FileStorageDto fileStorageDto = new FileStorageDto();
            BeanUtils.copyProperties(fileStorage,fileStorageDto);
            fileStorageDtos.add(fileStorageDto);
        }
        return fileStorageDtos;
    }


}
